package moodish.client;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import moodish.dummy.ClientCommDummy;

public class Window_Autentication extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JPanel contentPane;
	private JTextField txtInsertUsername;
	private JTextField txtInsertServerAddress;
	private JButton btnConnect = new JButton("Connect");

	private ClientCommDummy clientComm;
	private String username;

	/**
	 * Create the frame.
	 * 
	 * @param clientComm
	 */
	public Window_Autentication(ClientCommDummy clientComm) {
		this.clientComm = clientComm;
		this.setTitle("Login Window");
		buildGui();

	}

	private class Listener implements ActionListener {
		private Window_Autentication frame;

		private void setWB(Window_Autentication frame) {
			this.frame = frame;
		}

		@Override
		public void actionPerformed(ActionEvent e) {

			if (e.getSource().equals(btnConnect)) {
				username = txtInsertUsername.getText();
				clientComm.connect(txtInsertServerAddress.getText(), username);

				if (clientComm.isConnected()) {

					mainWindowBilder(frame);
				}
			}
		}
	}

	private void mainWindowBilder(Window_Autentication frame) {
		dispose();
		new Window_Main(clientComm, username);
	}

	private void buildGui() {
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 597, 358);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		txtInsertUsername = new JTextField();
		txtInsertUsername.setBounds(206, 129, 166, 26);
		contentPane.add(txtInsertUsername);
		txtInsertUsername.setColumns(10);

		Listener listener = new Listener();
		listener.setWB(this);
		btnConnect.setBackground(new Color(230, 230, 250));
		btnConnect.setBounds(222, 257, 115, 29);
		contentPane.add(btnConnect);
		btnConnect.addActionListener(listener);

		txtInsertServerAddress = new JTextField();
		txtInsertServerAddress.setBounds(206, 199, 166, 26);
		contentPane.add(txtInsertServerAddress);
		txtInsertServerAddress.setColumns(10);

		JLabel lblMoodishUserAutentication = new JLabel(
				"Moodish user autentication");
		lblMoodishUserAutentication.setFont(new Font("Book Antiqua",
				Font.PLAIN, 24));
		lblMoodishUserAutentication.setBounds(120, 32, 318, 20);
		contentPane.add(lblMoodishUserAutentication);

		JLabel lblInsertUsername = new JLabel("Insert Username");
		lblInsertUsername.setBounds(206, 109, 146, 20);
		contentPane.add(lblInsertUsername);

		JLabel lblInsertServerAddress = new JLabel("Insert Server Address");
		lblInsertServerAddress.setBounds(206, 172, 166, 20);
		contentPane.add(lblInsertServerAddress);
		setVisible(true);
	}

}
